﻿@{
    Layout = "~/Views/Shared/_level2.cshtml";
    if (false)
    {
        <script src="~/LiberUI/Scripts/jquery.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Base.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Action.js"></script>
    }
}
<title>系统管理-组织架构</title>
<link href="/SiteUI/Admin/share.css" rel="stylesheet" />
<link href="/SiteUI/Admin/Organize.css" rel="stylesheet" />
<script src="/SiteUI/res_sobiz.js"></script>
  <script src="~/LiberUI/Scripts/LiberUI.Base.js"></script>
@section contentnavi{
    <div id="appTitle">
        系统管理
    </div>
<ul id="slideNavi">
    <li><a class="Lite" href="/Admin/Company"><i class="icon-qiyerenzheng"></i>企业信息<i class="icon-navigatenext arrow-go"></i></a></li>
    <li><a class="Lite" href="/Admin/Organize"><i class="icon-wangluo1"></i>组织架构<i class="icon-navigatenext arrow-go"></i></a></li>
    <li><a class="Lite" href="/Admin/Accounts"><i class="icon-accountbox"></i>员工账号<i class="icon-navigatenext arrow-go"></i></a></li>
    <li><a class="Lite" href="/Admin/Authority"><i class="icon-security"></i>权限设置<i class="icon-navigatenext arrow-go"></i></a></li>
    <li><a class="Lite" href="/Admin/OpenApi"><i class="icon-swaphoriz"></i>开放接口<i class="icon-navigatenext arrow-go"></i></a></li>
    <li><a class="Lite" href="/Admin/Portal"><i class="icon-034library"></i>企业门户<i class="icon-navigatenext arrow-go"></i></a></li>
    <li><a class="Lite" href="/Admin/CloudApp"><i class="icon-nowwidgets"></i>应用管理<i class="icon-navigatenext arrow-go"></i></a></li>
</ul>
}

<div class="col-lg-4" id="orgTree_wraper">
    <div class="row">
        
    </div> 
</div>

<!--组织节点信息-->
<div class="col-lg-8" id="orgDetail">
    <div class="InfoBlock row">
        <div class="ib_head">
            <span class="ib_head_title">基本信息</span>
        </div>
        <div class="ib-body ">
            <div class="row">
                <div class="col-lg-2">
                    <h5>名称：</h5>
                </div>
                <div class="col-lg-10 " id="od-name">
                    <b>-</b><span id="CertifiedCompany"></span>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-2">
                    <h5>节点类型：</h5>
                </div>
                <div class="col-lg-5" id="od-type">
                    -
                </div>
                <div class="col-lg-5" id="od-certificated">
                    <h5><a>申请认证</a></h5>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-2">
                    <h5>上级节点：</h5>
                </div>
                <div class="col-lg-5" id="od-parent">
                    -
                </div>
                <div class="col-lg-5">
                    <a id="updateParentNode">变更上级</a>
                </div>
            </div>
            <div class="col-lg-2">
                <h5>描述：</h5>
            </div>
            <div class="col-lg-10" id="od-remark">
                -
            </div>
            <div class="col-lg-12" id="od-actions">
                <div class="button" id="btnAddChildCompany">
                    新增子公司
                </div>
                @*<div class="button" id="btnSetParent">
                    移动到...
                </div>*@
                <div class="button" id="btnAddChildDept">
                    新增子部门
                </div>
                <div class="button" id="btnEditOrg">
                    编辑
                </div>
                <div class="button btn-risky" id="btnRemove">
                    删除
                </div>
            </div>
        </div>
    </div>
</div>

<!--选定组织架构下的所有岗位-->
<div class="col-lg-8" id="orgPositions">
    <div class="row">
        <div class="FlateSectionHead">
            <h4><i class="icon-accountcircle"></i>岗位</h4>
        </div>
        <div class="col-lg-12" id="lstOrgPositions">

        </div>
    </div>
</div>
<div class="col-lg-7" id="posDetail">
    <div class="row" id="pvHead">
        <i class="icon-clear"></i>关闭
    </div>
    <div class="row" id="pvWraper">

    </div>
</div>

<div style="display:none">
    <div id="formNewOrg">
        <div class="FormRow row">
            <div class="col-lg-2 col-lg-offset-2 frm-title">
                <h5>上级节点：</h5>
            </div>
            <div class="col-lg-6 frm-value">
                <h4 id="norg-parent"></h4>
            </div>     
        </div>
        <div class="FormRow row">
            <div class="col-lg-2 col-lg-offset-2 frm-title">
                <h5>节点类型：</h5>
            </div>
            <div class="col-lg-6 frm-value">
                <h5 id="norg-type" >-</h5>
            </div>
        </div>
        <div class="FormRow row">
            <div class="col-lg-2 col-lg-offset-2 frm-title">
                <h5>名称：</h5>
            </div>
            <div class="col-lg-6 frm-value">
                <input maxlength="100" id="norg-name" />
            </div>        
        </div>
        <div class="FormRow row">
            <div class="col-lg-2 col-lg-offset-2 frm-title"><h5>描述：</h5></div>
            <div class="col-lg-6 frm-value">
                <textarea maxlength="1000" id="norg-remark"></textarea>
            </div>
        </div>
    </div>
    <div class="row PosItem">
        <div class="col-lg-3 pi-name"></div>
        <div class="col-lg-9 pi-more">
            <div class="row">
                <div class="col-lg-8 pi-remark">

                </div>
                <div class="col-lg-4 pi-action">
                    <a class="LinkTo" action="edit">编辑</a>
                    <a class="LinkTo" action="addChild">添加下级岗位</a>
                    <a class="LinkTo" action="remove" >删除</a>
                </div>
            </div>
        </div>
    </div>
    <div id="formPosEditor">
        <div class="FormRow row">
            <div class="col-lg-8 col-lg-offset-2">
                <h4 id="npos-parent"></h4>
            </div>
        </div>
        <div class="FormRow row">
            <div class="col-lg-2 col-lg-offset-2 frm-title">
                名称：
            </div>
            <div class="col-lg-6 frm-value">
                <input maxlength="100" id="npos-name" />
            </div>
        </div>
        <div class="FormRow row">
            <div class="col-lg-2 col-lg-offset-2 frm-title">描述：</div>
            <div class="col-lg-6 frm-value">
                <textarea maxlength="1000" id="npos-remark"></textarea>
            </div>
        </div>
    </div>
</div>
<div id="ChooseJob" style="display:none"></div>
<div id="chooseParNode">
    <div id="chengeNode" style="height:200px"></div>
</div>
<script>
    var Organize = {
        norg: {
            parentid: -1,
            name: "",
            remark: ""
        },
        ChangeNode:null,
        ChooseJob: null,
        objid:null,
        PersonnelList:null,
        parentName: null,
        corg: null,

        onOrgSlcted: function (org) {
            Organize.corg = org;
            Organize.showOrgInfo();
            if (!org.positions) {
                //TODO:如果没有岗位信息，则异步加载
                Organize.lstPositions.Loading("正在加载...");
                pvOrganizeTree.GetOrgPositions(org.idstr, Organize.onGetPositionsEnd);
            }
            Organize.showOrgPositions();
        },
        showOrgInfo: function () {
            $("#od-name>b").vl(Organize.corg.name);
            $("#od-remark").vl(Organize.corg.remark);
            $("#updateParentNode").show();
            if (Organize.corg.companystate == "Completed") {
                $("#od-type").vl("公司");
                if (Organize.corg.companyidstr != null && Organize.corg.companyidstr.length > 1) {
                    $("#CertifiedCompany").addClass("Company certificated").removeClass("no-certificated");
                    $("#CertifiedCompany").show();
                    $("#od-certificated a").vl("查看企业信息");
                    $("#od-certificated a").on("click", function () {
                        window.open("/Admin/Company");
                    });
                }
                else if (Organize.corg.companystate == "Pending") {
                    $("#CertifiedCompany").addClass("Company certificated").removeClass("no-certificated");
                    $("#CertifiedCompany").show();
                    $("#od-certificated a").vl("申请中");
                  
                }
                 else {
                    $("#CertifiedCompany").addClass("Company no-certificated").removeClass("certificated").show();
                    $("#od-certificated a").vl("申请认证");
                    $("#od-certificated a").on("click", function () {
                       //申请认证
                    });
                }
            } else{
                $("#od-type").vl("部门");
                $("#CertifiedCompany").removeClass("Company certificated");
            }

            var porg = pvOrganizeTree.orgtree.findObj("idstr", Organize.corg.parentidstr);
            if (porg) {
                $("#od-parent").vl(porg.name);
            }
            else {
                $("#od-parent").vl("-");
            }
        },
        showOrgPositions:function(){
            if (Organize.corg.positions) {
                Organize.lstPositions.vl(Organize.corg.positions)
                                     .update();
            }
            else {
                Organize.lstPositions.vl(null)
                                     .Empty($("<div class='empty'>").vl("<i></i>没有设置岗位，<a class='LinkTo' id='btnNewTopPosition' onclick='javascript:Organize.NewTopPosition()'>点这里</a>创建"))
                                     .update();
            }
        },
        onGetPositionsEnd: function (org) {
            //判断返回数据是否匹配当前选中的组织架构节点      
            if (org.idstr == Organize.corg.idstr) {
                //TODO:刷新岗位列表
                Organize.showOrgPositions();
            }
        },
        frmNewOrg: null,
        lstPositions:null,
        addChildDept: function (ev) {
            if (Organize.checkNewDept() == true) {
                pvOrganizeTree.addChildOrg(Organize.norg);
                Organize.frmNewOrg.hide();
            }
        },
        checkNewCompany: function () {
            Organize.norg.name = $("#norg-name").vl();
            Organize.norg.remark = $("#norg-remark").vl() || "未添加描述";
            if (Organize.norg.name.length == 0) {
                error("缺少信息", "请填写组织架构的名称");
                return false;
            }
            else {
                return true;
            }
        },
        checkNewDept: function () {
            Organize.norg.name = $("#norg-name").vl();
            Organize.norg.remark = $("#norg-remark").vl() || "未添加描述";
            if (Organize.norg.name.length == 0) {
                error("缺少信息", "请填写组织架构的名称");
                return false;
            }
            else {
                return true;
            }
        },
        editOrg: function (ev) {
            if ($("#norg-name").vl().length == 0) {
                error("缺少信息", "请填写组织架构的名称");
                return false;
            }else{
                Organize.corg.name = $("#norg-name").vl();
                Organize.corg.remark = $("#norg-remark").vl();
                pvOrganizeTree.updateOrg({ objid: Organize.corg.idstr, parentid: Organize.corg.parentidstr, name: Organize.corg.name, remark: Organize.corg.remark });
                Organize.showOrgInfo(Organize.corg);
                Organize.frmNewOrg.hide();
            } 
        },
        editOrgEnd: function (ev) {
            showOrgInfo(Organize.corg);
        },
        onPosSlcted:function(ev,data){
            //TODO:展示岗位详情面板
            if (!$("posDetail").hasClass("unfold")) {
                $("#orgTree_wraper").addClass("fold");
                $("#posDetail").addClass("unfold");
            }
            if (window.pvPositionDetail) {
                pvPositionDetail.LoadPosition(data.idstr); 
            }
            else {
                $("#pvWraper").load("/Admin/pvPositionDetail.html", null, function (){ pvPositionDetail.LoadPosition(data.idstr); });
            }
        },
        onPosAction: function (ev) {
            ev.stopPropagation();
            ev.preventDefault();
            var posItem = $(ev.currentTarget).parents(".PosItem").eq(0).data();
            var action = $(ev.currentTarget).attr("action");
            switch (action) {
                case "edit":
                    Organize.frmPosEditor.vl(posItem)
                                         .Title("编辑岗位")
                                         .RemoveBtn()
                                         .BtnEnter("确定", "", function (ev) {
                                             var cpos = Organize.frmPosEditor.vl();
                                             var pos = {
                                                 idstr: cpos.idstr,
                                                 name: $("#npos-name").vl(),
                                                 remark: $("#npos-remark").vl()
                                             };
                                             cpos.name = pos.name;
                                             cpos.remark = pos.remark;
                                             Organize.lstPositions.UpdateItem(cpos);
                                             Organize.frmPosEditor.hide();
                                             $.post("/Admin/UpdatePosition", pos);
                                         })
                                         .BtnEsc("取消")
                                         .update()
                                         .show();
                    $("#npos-name").vl(posItem.name).select();
                    $("#npos-remark").vl(posItem.remark);
                    break;
                case "addChild":
                    Organize.frmPosEditor.vl(posItem)
                                         .Title("新增下级岗位")
                                         .RemoveBtn()
                                         .BtnEnter("确定", "", function (ev) {
                                             var parent = Organize.frmPosEditor.vl();
                                             var pos = {
                                                 orgid: parent.orgidstr,
                                                 orgidstr:parent.orgidstr,
                                                 parentid: parent.idstr,
                                                 parentidstr: parent.idstr,
                                                 name : $("#npos-name").vl(),
                                                 remark: $("#npos-remark").vl()

                                             };
                                             Organize.frmPosEditor.disable();
                                             $.post("/Admin/NewPosition", pos, function (rtn) {
                                                 Organize.frmPosEditor.enable();
                                                 if (rtn.IsPositive) {
                                                     Organize.lstPositions.AppendItem(rtn.Data);
                                                 }
                                                 else {
                                                     error("操作失败",ErrFormate(res_sobiz, rtn.ErrorSrc, rtn.ErrorCode, rtn.Error));
                                                 }
                                                 Organize.frmPosEditor.hide();
                                             })
                                         })
                                         .BtnEsc("取消")
                                         .update()
                                         .show();
                    $("#npos-name").vl("").select();
                    $("#npos-remark").vl("");
                    break;
                case "remove":
                    var pos = $(this).parents('.PosItem').data();
                    if (pos) {
                        $.post("/Admin/DelPosition", { posid: pos.idstr }, function (rtn) {
                            if (rtn.IsPositive) {
                                //TODO：列表中同步更新
                                var del = rtn.Data.posidstr;
                                var tgtPos = Organize.lstPositions.vl().findObj("idstr", del);
                                Organize.lstPositions.RemoveItem(tgtPos);
                                Organize.corg.positions.removeBy("idstr", del);
                            }
                        })
                    }

                    break;
            }
        },
        NewTopPosition:function(ev){
            //TODO:创建组织架构下第一个顶级岗位
            Organize.frmPosEditor.Title("新增顶级岗位")
                                         .RemoveBtn()
                                         .BtnEnter("确定", "", function (ev) {
                                             var pos = {
                                                 orgid: Organize.corg.idstr,
                                                 orgidstr: Organize.corg.idstr,
                                                 parentid: -1,
                                                 parentidstr: "-1",
                                                 name: $("#npos-name").vl().trim(),
                                                 remark: $("#npos-remark").vl()
                                             };
                                             if (pos.name.length == 0) {
                                                 $("#npos-name").addClass("error");
                                                 error("错误", "请填写岗位名称");
                                                 return;
                                             }
                                             $("#npos-name").removeClass("error");
                                             Organize.frmPosEditor.disable();

                                             $.post("/Admin/NewPosition", pos, function (rtn) {
                                                 Organize.frmPosEditor.enable();
                                                 if (rtn.IsPositive) {
                                                     Organize.lstPositions.AppendItem(rtn.Data);
                                                 }
                                                 else {
                                                     error("操作失败", ErrFormate(res_sobiz, rtn.ErrorSrc, rtn.ErrorCode, rtn.Error));
                                                 }
                                                 Organize.frmPosEditor.hide();
                                             })
                                         })
                                         .BtnEsc("取消")
                                         .update()
                                         .show();
            $("#npos-name").vl("").select();
            $("#npos-remark").vl("");
        },
        frmPosEditor:null,
        tplPositionItem:null,
        _makePositionView: function ($cnt,pos) {
            var itm = Organize.tplPositionItem.clone();
            itm.children(".pi-name").vl(pos.name);
            itm.find(".pi-remark").vl(pos.remark);
            itm.data(pos);
            $cnt.append(itm);
        },
        chooseParNode: function () {
            Organize.UpdateParNode.show();
         
            Organize.ChangeNode.vl(pvOrganizeTree.orgtree)
                                       .update();
            $("#chengeNode .li-body").on("click", function () {
              //  Organize.objid = pvOrganizeTree.orgtree[$(this).attr("obj")];

            });
        },
        updateParentNode: function () {
            if (Organize.objid) {
                console.log(Organize.objid);
            }
            //  Organize.objid.     Organize.corg.idstr
        }
    }
    $(function () {      
        $("#orgTree_wraper>.row").load("/Admin/pvOrganizeTree.html", null, function () {
            if (window.pvOrganizeTree) {
                pvOrganizeTree.vf_onOrgSlcted = Organize.onOrgSlcted;
            }
        })

        $.getJSON("/Admin/GetCompanyAcnts", { company: -1 }, function (rtn) {//获得公司下所有员工
            if (rtn.IsPositive) {
                Organize.PersonnelList = rtn.Data.Acnts;
            }
        });
        var navis = $("#slideNavi>li");
        for (var i = 0; i < navis.length; i++) {
            if (navis.eq(i).children("a").attr("href") == window.location.pathname) {
                navis.eq(i).addClass("current");
                break;
            }
        };
        Organize.frmNewOrg = new LiberUI.Dialoger()
                                        .option("MainStyle", "Small")
                                        .Content($("#formNewOrg"));

        Organize.lstPositions = new LiberUI.Lister("#lstOrgPositions")
                                           .option("TreeMode", true)
                                           .option("NodePrefix","-")
                                           .option("ContentRender", Organize._makePositionView)
                                           .Empty("请选择一个组织架构来查看")
                                           .on("itemSlcted",Organize.onPosSlcted);

        Organize.frmPosEditor = new LiberUI.Dialoger()
                                           .option("MainStyle", "Small")
                                           .Title("新增岗位")
                                           .Content($("#formPosEditor"));

        Organize.tplPositionItem = $(".PosItem").eq(0);

        $("#btnEditOrg").on("click", function () {
            if (Organize.corg) {
                Organize.frmNewOrg
                        .Title("编辑")
                        .RemoveBtn()
                        .BtnEnter("确定", "", Organize.editOrg)
                        .BtnEsc("取消")
                        .update()
                        .show();
                $("#norg-parent").vl(Organize.parentName);
                $("#norg-remark").vl(Organize.corg.remark);
                $("#norg-name").vl(Organize.corg.name).select();
            }
            else {
                info("提示", "请选择要编辑的组织架构节点");
            }
        });

        $("#btnAddChildCompany").on("click", function () {
            if (Organize.corg) {
                dlgConfirm = new LiberUI.Dialoger()
                                       .Title("创建子公司")
                                       .Content($("<div style='width:300px'><h4>创建的公司需要通过平台管理方的审核。审核通过之前，您在组织架构树中暂时无法看到此公司</h4></div>"))
                                       .BtnEnter("继续", "", function () {
                                           window.open("/Company/NewCompany#" + Organize.corg.idstr, "_blank", "", true);
                                           dlgConfirm.hide().dispose();
                                       })
                                       .BtnEsc("放弃", "", function () {
                                           dlgConfirm.hide().dispose();
                                       })
                                       .update()
                                       .show();
            }
            else {
                error("缺少父级", "请选择父级组织架构节点",5);
            }
        });

        $("#btnAddChildDept").on("click", function () {
            if (Organize.corg) {
                Organize.frmNewOrg
                        .Title("新增子级部门")
                        .RemoveBtn()
                        .BtnEnter("确定", "", Organize.addChildDept)
                        .BtnEsc("取消")
                        .update()
                        .show();
                Organize.norg.parentid = Organize.corg.idstr;
                $("#norg-parent").vl(Organize.corg.name);
                $("#norg-name").vl("").focus();
                $("#norg-type").vl("部门");
                $("#norg-remark").vl("");
            }
            else {
                error("缺少父级", "请选择父级组织架构节点", 5);
            }
        });

        $("#btnRemove").on("click", function () {
            error("警告", "删除组织架构容易导致数据丢失，暂不对普通用户开放此功能",5);
            return;
            if (Organize.corg) {
                $.post("/Admin/DelOrganize", { orgid: Organize.corg.idstr }, function (rtn) {

                })
            }
        });
        Organize.ChooseJob = new LiberUI.Dialoger();
        Organize.UpdateParNode = new LiberUI.Dialoger()
                                          .option("MainStyle", "Small")
                                          .Content($("#chengeNode"))
                                          .Title("选择节点")
                                          .RemoveBtn()
                                          .BtnEnter("确定", "", function () {
                                              Organize.UpdateParNode.hide();
                                              Organize.updateParentNode();
                                          })
                                          .BtnEsc("取消", "", function () { Organize.UpdateParNode.hide(); })
                                          .update();
        Organize.ChangeNode = new LiberUI.Lister("#chengeNode")
                                         .option("TreeMode", true)
                                         .option("ContentRender", function ($item, o) {
                                             $("<div class='pot-orgItem'>").vl(o.name).appendTo($item);
                                         });

        $("body").on("click", function (ev) {
            $("#orgTree_wraper").removeClass("fold");
            $("#posDetail").removeClass("unfold");
        });
        $("#pvHead").on("click", function (ev) {
            $("#orgTree_wraper").removeClass("fold");
            $("#posDetail").removeClass("unfold");
        });
        $("#updateParentNode").on("click", Organize.chooseParNode);
        $("#lstOrgPositions").delegate("a", "click", {}, Organize.onPosAction);
        $("#posDetail").on("click", function (ev) { ev.stopPropagation(); });
       
    })
</script>

